package com.yunshangwoniu.core.utils;

import javax.servlet.http.HttpServletRequest;

public class RequestUtils
{
	public static String getIp(HttpServletRequest request)
	{
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
		}
		String[] ips = ip.replaceAll(" ", "").split(",");
		String firstIP = ip;
		if (ips.length > 2)
		{
			firstIP = ips[1];
		}
		else
		{
			firstIP = ips[0];
		}

		return firstIP;
	}
	
	/**
	 * 获得分页的url
	 * @param request
	 * @return
	 */
	public static String getPageUrl(HttpServletRequest request)
	{
		String queryString = request.getQueryString();
		queryString = queryString==null?"pageNo=1":queryString;
		String url = request.getRequestURI()+"?"+queryString;
		return url;
	}
	
	
}
